package com.stlm2.core.yun;

import com.stlm2.core.common.md5.MD5;
import com.stlm2.dems.entity.TiXianState;
import com.stlm2.util.Convert;
import com.stlm2.util.HttpsClient;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Date;

/**
 * 模板短信
 *
 */
public class Sms {

    private static Logger logger = LoggerFactory.getLogger(Sms.class);
	
	private static final String URL = Core.BASE_ACCOUNT_URL + "/SMS/TemplateSMS";
	
	public static String send(String to, String code){
		if(StringUtils.isBlank(code) || StringUtils.isBlank(to))
			return null;
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		json.put("templateId", "220479");
		json.put("to", to);
		JSONArray datas = new JSONArray();
		datas.put(code);
		datas.put("3分钟");
		json.put("datas", datas);
		
		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return null;
		
		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			String smsMessageSid = json.getJSONObject("templateSMS").getString("smsMessageSid");
			return smsMessageSid;
		}
		return null;
	}

	public static String sendNotice(String to, String orderCode, TiXianState tiXianState){
		if(StringUtils.isBlank(orderCode) || StringUtils.isBlank(to))
			return null;
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		if(tiXianState==TiXianState.Pass)
			json.put("templateId", "193537");
		if(tiXianState==TiXianState.NoPass)
			json.put("templateId", "193539");
		json.put("to", to);
		JSONArray datas = new JSONArray();
		datas.put(orderCode);
		json.put("datas", datas);

		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return null;

		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			String smsMessageSid = json.getJSONObject("templateSMS").getString("smsMessageSid");
			return smsMessageSid;
		}
		return null;
	}


	public static String sendNoticeYYB(String to, String text){
		if(StringUtils.isBlank(text) || StringUtils.isBlank(to))
			return null;
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		json.put("templateId", "220479");
		json.put("to", to);
		JSONArray datas = new JSONArray();
		datas.put(text);
		datas.put("3分钟");
		json.put("datas", datas);
		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return null;

		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			String smsMessageSid = json.getJSONObject("templateSMS").getString("smsMessageSid");
			return smsMessageSid;
		}
		return null;
	}
	/**
	 * Author：fuchunyu
	 * Date：2017/9/19
	 * Description：根据模板ID发送通知
	 */
	public static String sendNotice(String modelId,String to, String text[]){
		if(text.length<=0 || StringUtils.isBlank(to))
			return null;
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		json.put("templateId", modelId);
		json.put("to", to);
		JSONArray datas = new JSONArray();
		for (String s:text){
			datas.put(s);
		}
		json.put("datas", datas);
		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return null;

		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			String smsMessageSid = json.getJSONObject("templateSMS").getString("smsMessageSid");
			return smsMessageSid;
		}
		return null;
	}
	public static void main(String [] args) throws IOException {

		String yun_url="https://app.cloopen.com:8883";
		String yun_account_sid="8aaf07085c9b999c015caa63ec140904";
		String yun_auth_token="154316c9ccb842c4aa952687c1f9acac";
		String yun_app_id="8a216da85d158d1b015d1aaba7b0027b";
		String url=yun_url+"/2013-12-26/Accounts/"+yun_account_sid+"/SMS/TemplateSMS";
		int templateId=208680;
		String data="啥意思？";
		String phone="13353613565";

		JSONObject json = new JSONObject();
		json.put("appId", yun_app_id);
		json.put("templateId", templateId);
		json.put("to", phone);
		JSONArray datas = new JSONArray();
		datas.put(data);
		datas.put(data);
		json.put("datas", datas);


		Date date = new Date();
		url += "?sig=" + MD5.encryptMD5(yun_account_sid + yun_auth_token + Convert.toFullYMDHMSWithNoSplit(date));
		HttpPost post = new HttpPost(url);
		post.addHeader("Content-Type", "application/json;charset=utf-8;");
		post.addHeader("Accept", "application/json;");
		post.addHeader("Authorization", Base64.encodeBase64String((yun_account_sid + ":" + Convert.toFullYMDHMSWithNoSplit(date)).getBytes("UTF-8")));
		post.setEntity(new StringEntity(json.toString(), "UTF-8"));
		HttpClient client = HttpsClient.getClient(8883);

		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();
		String result = EntityUtils.toString(entity, "UTF-8");
		json = new JSONObject(result);
		System.out.println(json.getString("statusCode"));
		if("000000".equals(json.getString("statusCode"))){
			String smsMessageSid = json.getJSONObject("templateSMS").getString("smsMessageSid");
			System.out.println(smsMessageSid);
		}
		System.out.println(result);

	}
	
}